<?php
class SiteStructure_Extractor extends Base_Extractor
{
  const DEEP_LEVEL = 3;
  /**
   * Store Processed pages
   * @var array
   */
  public $processed   = array();
  public $count_page  = 0;
  
  private $stream;
  
  /**
   * Put site structure to selected file 
   * @param string $url
   * @param string $folder
   */
  public function putSiteStructuretoFile ($url, $folder)
  {
    $this->stream = fopen ($folder."/".$this->getDomain ($url), "wb+");

    fwrite($this->stream, '<?php $site=array();'."\n");

    $this->doParsePage($url, - 1);
  }
  /**
   * Method to get site structure with page options 
   * @param string $url
   */
  public function doParsePage($url, $level)
  {
    $level ++;
    $this->count_page ++;
    $this->processed[$url] = $level;
    $links = $this->getInnerLinks($url);
    
    foreach($links as $link)
    {
      if(! array_key_exists($link, $this->processed))
      {
        $this->processed[$link] = $level + 1;
      }
    }
        
    if($level < self::DEEP_LEVEL)
    {
      foreach($links as $link)
      {
        if(isset($this->processed[$link]) && is_numeric($this->processed[$link]))
        {
          if($this->processed[$link] == $level + 1)
          {
            $this->doParsePage($link, $level);
            
            $this->processed[$link] = null;
          }
        }
        continue;
      }
    }
    echo "Level: ", $level, " Link: ", $url, "\n";
    
    $inner_links = array_unique($this->getInnerLinks($url));
    
    $outer_links = array_unique($this->getOuterLinks($url));
    
    fwrite($this->stream, "\$site['".$url."'] = ".var_export(array("level" => $level, "links_outer" => $outer_links, "links_inner" => $inner_links ), true).";\n");
  }
}
?> 